import React, {Component} from 'react';
import {Image, Text, View} from 'react-native';
import RootSiblings from 'react-native-root-siblings';
import ToastContainer, {positions, durations} from './ToastContainer';

class Toast extends Component {
    static displayName = 'Toast';
    static propTypes = ToastContainer.propTypes;
    static positions = positions;
    static durations = durations;
    static sibling = null;

    static show = (message, options = {
        position: positions.CENTER,
        duration: durations.SHORT,
        shadow: false,
        opacity: 0.85,
        onHidden: () => null,
        onShown: () => null,
    }) => {
        Toast.hide();
        Toast.sibling = new RootSiblings(
            <ToastContainer
                {...options}
                visible={true}
                onDestroy={()=>{
                    Toast.hide();
                }}
            >
                {message}
            </ToastContainer>,
        );
    };

    static success = (message, options = {
        position: positions.CENTER,
        duration: durations.SHORT,
        shadow: false,
        opacity: 0.85,
        onHidden: () => null,
        onShown: () => null,
    }) => {
        Toast.hide();
        Toast.sibling = new RootSiblings(
            <ToastContainer
                {...options}
                visible={true}
                onDestroy={()=>{
                    Toast.hide();
                }}
            >
                <View style={{
                    justifyContent: 'center',
                    alignItems: 'center',
                    padding: 10,
                }}>
                    <Image source={require('./icons/success.png')} style={{
                        width: 40,
                        height: 40,
                        tintColor: '#fff',
                    }}/>
                    <Text style={{
                        color: '#fff',
                        fontSize: 16,
                        textAlign: 'center',
                        marginTop: 15,
                    }}>{message}</Text>
                </View>
            </ToastContainer>);
    };

    static fail = (message, options = {
        position: positions.CENTER,
        duration: durations.SHORT,
        shadow: false,
        opacity: 0.85,
        onHidden: () => null,
        onShown: () => null,
    }) => {
        Toast.hide();
        Toast.sibling = new RootSiblings(
            <ToastContainer
                {...options}
                visible={true}
                onDestroy={()=>{
                    Toast.hide();
                }}
            >
                <View style={{
                    justifyContent: 'center',
                    alignItems: 'center',
                    padding: 10,
                }}>
                    <Image source={require('./icons/fail.png')} style={{
                        width: 40,
                        height: 40,
                        tintColor: '#fff',
                    }}/>
                    <Text style={{
                        color: '#fff',
                        fontSize: 16,
                        textAlign: 'center',
                        marginTop: 15,
                    }}>{message}</Text>
                </View>
            </ToastContainer>);
    };

    static hide = () => {
        if (Toast.sibling instanceof RootSiblings) {
            Toast.sibling.destroy();
        } else {
            //console.warn(`Toast.hide expected a \`RootSiblings\` instance as argument.\nBut got \`${typeof toast}\` instead.`);
        }
    };

    render() {
        return null;
    }
}

export {
    RootSiblings as Manager,
};
export default Toast;
